package GUI;


import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;

import client.ClientConsole;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.awt.Font;

/**this class add new copy to db*/
public class GUI_AddCopy extends JPanel
{
	/**textField to get copy id to add*/
	private JTextField textField;
	/**textField to get book id that we want to add a copy of it to db*/
	private JTextField textField_1;
	/** clc instance of ClientConsole*/
	private ClientConsole clc;
	
	/**@param cc current ClientConsole
	 * @param gl instance of current GUI_LogIn
	 * @param gc instance of current GUI_Catalog*/
	public GUI_AddCopy(ClientConsole cc,final GUI_LogIn gl,final GUI_Catalog gc) {
		setLayout(null);
		clc=cc;
		this.setSize(700, 600);
		this.setName("Add Book Copy");
		
		JLabel lblCopyId = new JLabel("Copy Id");
		lblCopyId.setBounds(36, 81, 46, 14);
		add(lblCopyId);
		
		JLabel lblBookId = new JLabel("Book Id");
		lblBookId.setBounds(36, 121, 46, 14);
		add(lblBookId);
		
		textField = new JTextField();
		textField.setBounds(119, 78, 160, 20);
		add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(119, 118, 160, 20);
		add(textField_1);
		textField_1.setColumns(10);
		
		JButton btnAdd = new JButton("Add");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String cId=textField.getText();
				String bId=textField_1.getText();
				
				try {
					String s[]={"addCopy",cId,bId}; 
					for(int i=1;i<s.length;i++)
					{
						if(s[i].equals(""))
						{
							JOptionPane.showMessageDialog(new JFrame(),"Please fill al fields");
							return;
						}
					}
					clc.client.sendToServer(s);
					while(clc.client.flag==0)
						System.out.print("");;
					clc.client.flag=0;
					JOptionPane.showMessageDialog(new JFrame(),clc.client.message);
					textField.setText("");
					textField_1.setText("");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btnAdd.setBounds(171, 193, 89, 23);
		add(btnAdd);
		
		JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gl.setContentPane(gc);
			}
		});
		btnBack.setBounds(285, 193, 89, 23);
		add(btnBack);
		
		JButton btnLogOut = new JButton("Log Out");
		btnLogOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gl.LogOut();
			}
		});
		btnLogOut.setBounds(407, 193, 89, 23);
		add(btnLogOut);
		
		JLabel lblPleaseFillAll = new JLabel("Please fill all the fields:");
		lblPleaseFillAll.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblPleaseFillAll.setBounds(36, 36, 211, 20);
		add(lblPleaseFillAll);
		this.setVisible(true);
	}
}
